/**
 * @author 愚人码头
 * 文本域字符数判断
 * 更多http://www.css88.com/archives/3605
 */
(function($){
    $.fn.chackTextarea = function(options) {
        var opts = $.extend({},$.fn.chackTextarea.deflunt,options);
        //将中文视为一个字符，将英文视为半个字符，也就是两个英文字符按一个字符计算。
        var chackNums=opts.chackNum*2;
        //设置初始文本
        $(this).find(opts.chackNumObj).html("你还能输入<em>" +opts.chackNum +"</em>个字!");
        var chackText=function($this){
            //清除timer
            $(opts.chackObj).blur(function(){
                clearInterval(this.timer);
            });
            $(opts.chackObj).focus(function(){
                var timer=setInterval(function(){
                    var newvalue = $this.find(opts.chackObj).val().replace(/[^\x00-\xff]/g, "**");
                    if(newvalue.length>=0){
                        if (newvalue.length > chackNums) {
                            $this.find(opts.chackNumObj).html("已超出<em>" +Math.ceil((newvalue.length - chackNums)/2) +"</em>个字!");
                            $this.find(opts.chackNumObj).addClass(opts.errorClass);
                            $this.find(opts.chackBtn).attr("disabled","disabled");
                            $this.find(opts.chackBtn).addClass(opts.disabledClass);
                        }else{
                            $this.find(opts.chackNumObj).html("你还能输入<em>" +Math.ceil((chackNums-newvalue.length)/2) +"</em>个字!");
                            $this.find(opts.chackNumObj).removeClass(opts.errorClass);
                            $this.find(opts.chackBtn).attr("disabled","");
                            $this.find(opts.chackBtn).removeClass(opts.disabledClass);
                        }
                    }else{
                        $this.find(opts.chackBtn).removeClass(opts.disabledClass);
                        $this.find(opts.chackNumObj).removeClass(opts.errorClass);
                    }
                },500);
                return this;
            });

        };
        return $(this).each(function() {
            chackText($(this));
        });
    };
    $.fn.chackTextarea.deflunt={
		chackNum : 140,
        chackObj:".chackTextarea-area",
        chackNumObj :".chackTextarea-num",
        chackBtn:".chackTextarea-btn",
        disabledClass:"chackTextarea-disabled",
        errorClass:"chackTextarea-errortxt"
	};
})(jQuery);